-
Notifications
You must be signed in to change notification settings - Fork 1.9k
vscode: migrate to more type-safe assert impl #3372
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
vscode: migrate to more type-safe assert impl #3372
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Meh. I've spotted three uses of assert:
- one checks that the
Content-Lengthheader from GitHub is a number- but GitHub could very well use
Transfer-Encoding: chunkedwithout anyContent-Lengthheader, so this should not be an assertion; we could show an indeterminate progress bar if that happens
- but GitHub could very well use
- one checks that the binary runs
- but we could have uploaded a wrong binary to GitHub, or the extension might have downloaded the wrong one (ahem Linux/ARM)
- one checks that we didn't get an error when creating the destination directory
- but this might as well happen in day-to-day usage (e.g. when running out of disk space)
I'm used to assertions meaning "this couldn't have happened unless the program state is inconsistent", instead of "this bad thing outside of our control happened and we wish it hadn't".
So the last two of those cases would probably be served better by a notification, and the first is pretty harmless but should probably be handled for robustness.
editors/code/src/util.ts
Outdated
| import * as vscode from "vscode"; | ||
| import { strict as nativeAssert } from "assert"; | ||
|
|
||
| export function assert(condition: unknown, explanation: string): asserts condition { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think a single assert(condition: boolean, explanation: string) should be enough.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, do you mean boolean? If yes, we would need to explicitly use null comparisongs assert(obj == null) vs assert(obj), or is this desired?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think explicit boolean is good
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
|
@lnicola Even that Anyway, we should be able to catch assertion errors and display them to the user. In fact throw new Error("Sanity check was not successful"); |
27e561d to
6dc598f
Compare
|
bors r+ |
Build succeeded |

https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions